package aiku.numericalcontroloa.common.base;

import aiku.numericalcontroloa.common.utils.SpringContextUtil;
import aiku.numericalcontroloa.common.utils.StringUtils;
import com.github.pagehelper.PageHelper;

import javax.servlet.http.HttpServletRequest;

/**
 * 分页数据
 */
public class PageData {
    /**
     * 当前记录起始索引
     */
    public static final String PAGE_NUM = "pageNum";

    /**
     * 每页显示记录数
     */
    public static final String PAGE_SIZE = "pageSize";

    /**
     * 排序列
     */
    public static final String ORDER = "orderBy";

    /**
     * 封装分页对象
     */
    public static BaseParams getPageDomain() {
        HttpServletRequest httpServletRequest = SpringContextUtil.getHttpServletRequest();
        BaseParams pageDomain = new BaseParams();
        String pageNum = httpServletRequest.getParameter(PAGE_NUM);
        String pageSize = httpServletRequest.getParameter(PAGE_SIZE);
        String orderBy = httpServletRequest.getParameter(ORDER);
        if (StringUtils.isEmpty(pageNum)) {
            pageNum = "1";
        }
        if (StringUtils.isEmpty(pageSize)) {
            pageSize = "10";
        }
        if (StringUtils.isEmpty(orderBy)) {
            orderBy = "";
        }
        pageDomain.setPageNum(Integer.valueOf(pageNum));
        pageDomain.setPageSize(Integer.valueOf(pageSize));
        pageDomain.setOrderBy(StringUtils.camelToUnderline(orderBy));
        return pageDomain;
    }

    /**
     * 设置请求分页数据
     */
    public static void startPage() {
        BaseParams pageDomain = PageData.getPageDomain();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        if (!org.springframework.util.StringUtils.isEmpty(pageNum) && !org.springframework.util.StringUtils.isEmpty(pageSize)) {
            PageHelper.startPage(pageNum, pageSize, pageDomain.getOrderBy());
        }
    }
}
